package com.kkd.kkdapp.adapter;

import com.kkd.kkdapp.activity.KKDWebActivity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class CycleViewPagerAdapter extends PagerAdapter {
	private int[] images;
	private Context context;
	private Dialog dialog;

	public CycleViewPagerAdapter(Context context, int[] images, Dialog dialog) {
		this.context = context;
		this.images = images;
		this.dialog = dialog;
	}

	@Override
	public int getCount() {
		return images.length;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	public void destroyItem(android.view.ViewGroup container, int position,
			Object object) {
		container.removeView((View) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		RelativeLayout layout = new RelativeLayout(context);
		layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));

		ImageView imageView = new ImageView(context);
		imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setBackgroundResource(images[position]);
		layout.addView(imageView);
		if (images[position] == images[images.length - 1]) {
			layout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, KKDWebActivity.class);
					intent.putExtra("url",
							com.kkd.kkdapp.Environment.getH5Url()
									+ "safety.html"); // 正式
					intent.putExtra("titleName", "安全保障");
					context.startActivity(intent);
					if (dialog != null) {
						dialog.cancel();
						dialog = null;
					}
				}
			});
		}
		container.addView(layout);
		return layout;
	}
}
